package com.mj.haier.rrskx.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.rrskx.domain.CarsTeamInfo;
import com.mj.haier.rrskx.repository.CarsTeamInfoRepository;
import com.mj.haier.rrskx.repository.search.CarsTeamInfoSearchRepository;
import com.mj.haier.rrskx.web.rest.errors.BadRequestAlertException;
import com.mj.haier.rrskx.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CarsTeamInfo.
 */
@RestController
@RequestMapping("/api")
public class CarsTeamInfoResource {

    private final Logger log = LoggerFactory.getLogger(CarsTeamInfoResource.class);

    private static final String ENTITY_NAME = "microRrskxCarsTeamInfo";

    private final CarsTeamInfoRepository carsTeamInfoRepository;

    private final CarsTeamInfoSearchRepository carsTeamInfoSearchRepository;

    public CarsTeamInfoResource(CarsTeamInfoRepository carsTeamInfoRepository, CarsTeamInfoSearchRepository carsTeamInfoSearchRepository) {
        this.carsTeamInfoRepository = carsTeamInfoRepository;
        this.carsTeamInfoSearchRepository = carsTeamInfoSearchRepository;
    }

    /**
     * POST  /cars-team-infos : Create a new carsTeamInfo.
     *
     * @param carsTeamInfo the carsTeamInfo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new carsTeamInfo, or with status 400 (Bad Request) if the carsTeamInfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    // @PostMapping("/cars-team-infos")
    // @Timed
    // public ResponseEntity<CarsTeamInfo> createCarsTeamInfo(@RequestBody CarsTeamInfo carsTeamInfo) throws URISyntaxException {
    //     log.debug("REST request to save CarsTeamInfo : {}", carsTeamInfo);
    //     if (carsTeamInfo.getCarsTeamId() != null) {
    //         throw new BadRequestAlertException("A new carsTeamInfo cannot already have an carsTeamId", ENTITY_NAME, "idexists");
    //     }
    //     CarsTeamInfo result = carsTeamInfoRepository.save(carsTeamInfo);
    //     carsTeamInfoSearchRepository.save(result);
    //     return ResponseEntity.created(new URI("/api/cars-team-infos/" + result.getCarsTeamId()))
    //         .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getCarsTeamId().toString()))
    //         .body(result);
    // }

    /**
     * PUT  /cars-team-infos : Updates an existing carsTeamInfo.
     *
     * @param carsTeamInfo the carsTeamInfo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated carsTeamInfo,
     * or with status 400 (Bad Request) if the carsTeamInfo is not valid,
     * or with status 500 (Internal Server Error) if the carsTeamInfo couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    // @PutMapping("/cars-team-infos")
    // @Timed
    // public ResponseEntity<CarsTeamInfo> updateCarsTeamInfo(@RequestBody CarsTeamInfo carsTeamInfo) throws URISyntaxException {
    //     log.debug("REST request to update CarsTeamInfo : {}", carsTeamInfo);
    //     if (carsTeamInfo.getCarsTeamId() == null) {
    //         throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
    //     }
    //     CarsTeamInfo result = carsTeamInfoRepository.save(carsTeamInfo);
    //     carsTeamInfoSearchRepository.save(result);
    //     return ResponseEntity.ok()
    //         .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, carsTeamInfo.getCarsTeamId().toString()))
    //         .body(result);
    // }

    /**
     * GET  /cars-team-infos : get all the carsTeamInfos.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of carsTeamInfos in body
     */
    // @GetMapping("/cars-team-infos")
    // @Timed
    // public List<CarsTeamInfo> getAllCarsTeamInfos() {
    //     log.debug("REST request to get all CarsTeamInfos");
    //     return carsTeamInfoRepository.findAll();
    // }

    /**
     * GET  /cars-team-infos/:carsTeamId : get the "carsTeamId" carsTeamInfo.
     *
     * @param carsTeamId the carsTeamId of the carsTeamInfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the carsTeamInfo, or with status 404 (Not Found)
     */
    @GetMapping("/cars-team-infos/{carsTeamId}")
    @Timed
    public ResponseEntity<CarsTeamInfo> getCarsTeamInfo(@PathVariable String carsTeamId) {
        log.debug("REST request to get CarsTeamInfo : {}", carsTeamId);
        Optional<CarsTeamInfo> carsTeamInfo = carsTeamInfoRepository.findByCarsTeamId(carsTeamId);
        return ResponseUtil.wrapOrNotFound(carsTeamInfo);
    }

    /**
     * GET  /cars-team-infos/byNetpoint/: netpoint : get the carsTeamInfos by netpoint
     *
     * @param netpoint
     * @return the ResponseEntity with status 200 (OK) and the list of carsTeamInfos in body
     */
    @GetMapping("/cars-team-infos/byNetpoint/{netpoint}")
    @Timed
    public List<CarsTeamInfo> searchCarsTeamInfosByNetpoint(@PathVariable String netpoint){
        log.debug("REST request to search CarsTeamsInfos by netpoint and validateFlag = 1 : {}", netpoint);
        // 有效性:0:无效,1:有效
        String validateFlag = "1";
        return carsTeamInfoRepository.findAllByNetpointAndValidateFlag(netpoint, validateFlag);
    }


    /**
     * DELETE  /cars-team-infos/:carsTeamId : delete the "carsTeamId" carsTeamInfo.
     *
     * @param carsTeamId the carsTeamId of the carsTeamInfo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    // @DeleteMapping("/cars-team-infos/{carsTeamId}")
    // @Timed
    // public ResponseEntity<Void> deleteCarsTeamInfo(@PathVariable String carsTeamId) {
    //     log.debug("REST request to delete CarsTeamInfo : {}", carsTeamId);

    //     carsTeamInfoRepository.deleteByCarsTeamId(carsTeamId);
    //     carsTeamInfoSearchRepository.deleteByCarsTeamId(carsTeamId);
    //     return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, carsTeamId.toString())).build();
    // }

    /**
     * SEARCH  /_search/cars-team-infos?query=:query : search for the carsTeamInfo corresponding
     * to the query.
     *
     * @param query the query of the carsTeamInfo search
     * @return the result of the search
     */
    // @GetMapping("/_search/cars-team-infos")
    // @Timed
    // public List<CarsTeamInfo> searchCarsTeamInfos(@RequestParam String query) {
    //     log.debug("REST request to search CarsTeamInfos for query {}", query);
    //     return StreamSupport
    //         .stream(carsTeamInfoSearchRepository.search(queryStringQuery(query)).spliterator(), false)
    //         .collect(Collectors.toList());
    // }

}
